import java.util.ArrayList;
import java.util.List;

public class Solution2276 {

    List<int[]> list=new ArrayList<>();
    int count=0;

    public Solution2276() {

    }

    public void add(int left, int right) {
        if (list.isEmpty() || left>list.get(list.size()-1)[1]){
            list.add(new int[]{left,right});
            count+=right-left+1;
            return;
        }
        int l=0,r=list.size()-1,ans=-1;
        while (l<=r){
            int mid=(l+r)/2;
            if (left<=list.get(mid)[1]){
                ans=mid;
                r=mid-1;
            }else {
                l=mid+1;
            }
        }
        if (right<list.get(ans)[0]){
            list.add(ans,new int[]{left,right});
            count+=right-left+1;
        }else{
            if (left<list.get(ans)[0]) {
                count += list.get(ans)[0] - left;
                list.get(ans)[0] = left;
            }
            int t=ans+1;
            while (t<list.size() && right>=list.get(t)[0]){
                count+=list.get(t)[0]-list.get(t-1)[1]-1;
                t++;
            }
            if (list.get(t-1)[1]<right){
                count+=right-list.get(t-1)[1];
                list.get(ans)[1]=right;
            }else {
                list.get(ans)[1]=list.get(t-1)[1];
            }
            for (int i=0;i<t-ans-1;i++){
                list.remove(ans+1);
            }
        }
    }

    public int count() {
        return count;
    }

    public static void main(String[] args) {
        Solution2276 solution2276=new Solution2276();
        solution2276.add(5,6);
        solution2276.add(7,8);
        solution2276.add(5,10);
        solution2276.add(4,4);
        System.out.println(solution2276.count);
    }
}
